package com.wanpinghui.supplier.user.company;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.object.HomePageActivity;
import com.wanpinghui.supplier.user.UpdateUserInfoActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.CompanyBean;
import com.wanpinghui.wphlibrary.model.user.bean.CourseBean;
import com.wanpinghui.wphlibrary.model.user.bean.SkillBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.manager.MActivityManager;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * Created by tg596 on 2018/1/29.
 */
@ContentView(R.layout.layout_service_specialty)
public class ServiceSpecialtyActivity extends BaseActivity {

    @ViewInject(R.id.mViewBacktrack)
    private ImageView mViewBacktrack;

    @ViewInject(R.id.btn_complete)
    private ThrottleButton btn_complete;

    @ViewInject(R.id.ll_skill)
    private LinearLayout ll_skill;

    @ViewInject(R.id.btn_submit)
    private ThrottleButton btn_submit;

    @ViewInject(R.id.edt_company_info)
    private YunShlEditText edt_company_info;

    @ViewInject(R.id.tv_payments)
    private TextView tv_payments;

    @ViewInject(R.id.tv_delivery)
    private TextView tv_delivery;

    @ViewInject(R.id.tv_warning_text)
    private TextView tv_warning_text;

    private List<SkillBean> skillList;

    private CompanyBean bean;



    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        bean = getIntent().getParcelableExtra("bean");
        if (bean != null){
            if (bean.getAudit_status() == 21 ) {
                tv_warning_text.setVisibility(View.VISIBLE);
                tv_warning_text.setText(bean.getRemark());
            }
            skillList = bean.getSkillList();
            if (skillList != null && skillList.size() > 0) {
                for (int i = 0; i < skillList.size(); i++) {
                    ll_skill.addView(createView(skillList.get(i), i));
                }
            }
        }
    }

    @Override
    public void bindEvents() {

        mViewBacktrack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });

        btn_complete.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CompanyBean companyBean = new CompanyBean();
                if (bean != null)
                    companyBean.setId(bean.getId());
                companyBean.setSkillList(skillList);
                WPHSdk.getService(UserInfoService.class)
                        .saveCompanySkill(companyBean, new YRequestCallback<CompanyBean>() {
                            @Override
                            public void onSuccess(CompanyBean var1) {
                                MActivityManager.getInstance().delACT(ServiceSpecialtyActivity.class);
                                MActivityManager.getInstance().delACT(CompanyNameActivity.class);
                                MActivityManager.getInstance().delACT(BusinessInformationActivity.class);
                                MActivityManager.getInstance().delACT(CompanyCaseCourseActivity.class);
                                MActivityManager.getInstance().delACT(DevelopmentPathActivity.class);
                                MActivityManager.getInstance().delACT(CompanyInfoEditActivity.class);
                                MActivityManager.getInstance().delACT(AddCaseActivity.class);
                                MActivityManager.getInstance().delACT(CompanyBaseInfoActivity.class);
                                Intent intent = new Intent(ServiceSpecialtyActivity.this, CompanyInfoEditActivity.class);
                                intent.putExtra("editFinish",true);
                           startActivity(intent);
                            }

                            @Override
                            public void onFailed(int var1, String message) {
                                showWarningText(message);
                            }

                            @Override
                            public void onException(Throwable var1) {

                            }
                        });
            }
        });

        btn_submit.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtil.isEmpty(edt_company_info.getTextString())){
                    showWarningText("请输入内容");
                    return;
                }
                if (skillList == null){
                    skillList = new ArrayList<>();
                }
                ll_skill.removeAllViews();
                SkillBean  skillBean = new SkillBean();
                if (bean != null)
                    skillBean.setCompany_id(bean.getId());
                skillBean.setSkill_name(edt_company_info.getTextString());
                skillList.add(skillBean);
                for (int i = 0 ; i <skillList.size();  i ++){
                    ll_skill.addView(createView(skillList.get(i),i));
                }

            }
        });
        tv_payments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_company_info.setText(tv_payments.getText().toString());
            }
        });
        tv_delivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edt_company_info.setText(tv_delivery.getText().toString());
            }
        });

    }

    private View createView(SkillBean s,final  int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_skill,null);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
        ImageView iv_delete = (ImageView)view.findViewById(R.id.iv_delete);
        tv_name.setText(s.getSkill_name());
        iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                skillList.remove(i);
                ll_skill.removeAllViews();
                for (int i = 0 ; i <skillList.size();  i ++){
                    ll_skill.addView(createView(skillList.get(i),i));
                }
            }
        });
        return view;
    }

    @Override
    public void initData() {

    }
}
